package com.team.service.impl;

import com.team.dao.AdminMapper;
import com.team.dao.DeptMapper;
import com.team.dao.DutyMapper;
import com.team.entity.Admin;
import com.team.entity.Dept;
import com.team.entity.Duty;
import com.team.service.ManaginePrivilegesService;
import com.team.util.WebUtil;
import com.team.vo.CountPrice;
import com.team.vo.DoctorDate;
import com.team.vo.Result;
import com.team.vo.Scheduling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author keluoyisi
 * @program: seven
 * @description: 对SectionWService的实现
 * @date 2022-10-14 10:26:06
 */
@Service
public class ManaginePrivilegesServiceImpl implements ManaginePrivilegesService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private DutyMapper dutyMapper;
    /**
     * @param :
     * @return Result
     * @description 显示所有部门以及对应的科室
     */
    @Override
    public Result selectDeptSection() {
        List<Dept> depts = deptMapper.queryAllDepts();
        return new Result(200,"查询成功",depts);
    }

    /**
     * @param :
     * @return Result
     * @description 根据科室id查询可是对应的所有医生  医生的排班的详细信息（价格，时间，预约数）
     */
    @Override
    public Result selectSectionDoctors() {
        //把科室的id记录在session域中
        int id = (int) WebUtil.getSession().getAttribute("sectionId");
//        int id = 6;
        ArrayList<Scheduling> list = getAdmins(id);
        return new Result(200,"成功",list);
    }

    /**
     * @param :
     * @return Result
     * @description 根据科室id查询可是对应的所有医生  医生的排班的详细信息（价格，时间，预约数）
     */
    @Override
    public Result selectSectionDoctors2() {
        //把科室的id记录在session域中
        int id = (int) WebUtil.getSession().getAttribute("appointmentSectionId");
//        int id = 6;
        ArrayList<Scheduling> list = getAdmins(id);
        return new Result(200,"成功",list);
    }

    public ArrayList<Scheduling> getAdmins(int id){
        //该科室所有医生的头像，姓名，详细
        List<Admin> admins = adminMapper.selectSectionId(id);
        System.out.println("admin========================="+admins);
        //医生的排班的详细信息（价格，时间，预约数）
        List<Duty> duties = dutyMapper.selectSectionId(id);
        System.out.println(duties);

        //获取到近七天的时间
        List<String> currentTimes = getCurrentTimes();
        ArrayList<Scheduling> list = new ArrayList<>();
        for (Admin admin : admins) {
            Scheduling scheduling = new Scheduling();
            scheduling.setId(admin.getId());
            scheduling.setAccount(admin.getAccount());
            scheduling.setImg(admin.getHardUrl());
            scheduling.setName(admin.getName());
            scheduling.setIntroduction(admin.getIntroduction());

            for (Duty duty : duties) {
                int i = 1;
                if (admin.getAccount().equals(duty.getAccount())){
                    System.out.println();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = duty.getStuyDatatime();
                    String format = dateFormat.format(date);
                    for (String currentTime : currentTimes) {
                        if (format.equals(currentTime)){
                            if(i==1){
                                scheduling.setDid1(duty.getId());
                                scheduling.setDate1(i);
                                scheduling.setCount1(duty.getVisitCount());
                                scheduling.setNum1(duty.getDefaultNum());
                                scheduling.setDate11(currentTime);
                                scheduling.setPrice1(duty.getPrice());
                            }
                            if(i==2){
                                scheduling.setDid2(duty.getId());
                                scheduling.setDate2(i);
                                scheduling.setCount2(duty.getVisitCount());
                                scheduling.setNum2(duty.getDefaultNum());
                                scheduling.setDate22(currentTime);
                                scheduling.setPrice2(duty.getPrice());
                            }
                            if(i==3){
                                scheduling.setDid3(duty.getId());
                                scheduling.setDate3(i);
                                scheduling.setCount3(duty.getVisitCount());
                                scheduling.setNum3(duty.getDefaultNum());
                                scheduling.setDate33(currentTime);
                                scheduling.setPrice3(duty.getPrice());
                            }
                            if(i==4){
                                scheduling.setDid4(duty.getId());
                                scheduling.setDate4(i);
                                scheduling.setCount4(duty.getVisitCount());
                                scheduling.setNum4(duty.getDefaultNum());
                                scheduling.setDate44(currentTime);
                                scheduling.setPrice4(duty.getPrice());
                            }
                            if(i==5){
                                scheduling.setDid5(duty.getId());
                                scheduling.setDate5(i);
                                scheduling.setCount5(duty.getVisitCount());
                                scheduling.setNum5(duty.getDefaultNum());
                                scheduling.setDate55(currentTime);
                                scheduling.setPrice5(duty.getPrice());
                            }
                            if(i==6){
                                scheduling.setDid6(duty.getId());
                                scheduling.setDate6(i);
                                scheduling.setCount6(duty.getVisitCount());
                                scheduling.setNum6(duty.getDefaultNum());
                                scheduling.setDate66(currentTime);
                                scheduling.setPrice6(duty.getPrice());
                            }
                            if(i==7){
                                scheduling.setDid7(duty.getId());
                                scheduling.setDate7(i);
                                scheduling.setCount7(duty.getVisitCount());
                                scheduling.setNum7(duty.getDefaultNum());
                                scheduling.setDate77(currentTime);
                                scheduling.setPrice7(duty.getPrice());
                            }
                        }
                        if(i==1){
                            scheduling.setDate11(currentTime);
                        }
                        if(i==2){
                            scheduling.setDate22(currentTime);
                        }
                        if(i==3){
                            scheduling.setDate33(currentTime);
                        }
                        if(i==4){
                            scheduling.setDate44(currentTime);
                        }
                        if(i==5){
                            scheduling.setDate55(currentTime);
                        }
                        if(i==6){
                            scheduling.setDate66(currentTime);
                        }
                        if(i==7){
                            scheduling.setDate77(currentTime);
                        }
                        i++;
                    }
                }
                else {
                    for (String currentTime : currentTimes) {
                        if(i==1){
                            scheduling.setDate11(currentTime);
                        }
                        if(i==2){
                            scheduling.setDate22(currentTime);
                        }
                        if(i==3){
                            scheduling.setDate33(currentTime);
                        }
                        if(i==4){
                            scheduling.setDate44(currentTime);
                        }
                        if(i==5){
                            scheduling.setDate55(currentTime);
                        }
                        if(i==6){
                            scheduling.setDate66(currentTime);
                        }
                        if(i==7){
                            scheduling.setDate77(currentTime);
                        }
                        i++;
                    }

                }
            }

            list.add(scheduling);
        }
        System.out.println("list======================"+list);

        return list;
    }

    /**
     * @param :
     * @return Result
     * @description  直接获取的时间—日期（yyyy-MM-dd）连续的7天
     */
    @Override
    public List<String> getCurrentTimes() {
        List<String> times = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String format = dateFormat.format(date);
        Date parse=null;
        try {
            parse = dateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for (int i=0;i<7;i++){
            String day = getNextDay(parse, i);
            times.add(day);
        }
        return times;
    }

    /**
     * @param id: admin的id
     * @return Result
     * @description  对医生进行排班
     */
    @Override
    public Result schedulingAdminId(int id) {
        Admin manager = (Admin) WebUtil.getSession().getAttribute("Admin");
        int sectionId = (int) WebUtil.getSession().getAttribute("sectionId");
        //具体的参数参数前端设计好再设置
        return new Result(200,"成功");
    }

    /**
     * @param id:
     * @return Result
     * @description 得到科室的id并且存在session中
     */
    @Override
    public Result addSectionId(int id) {
        WebUtil.getSession().setAttribute("sectionId",id);
        return new Result(200,"添加成功");
    }

    /**
     * @param account:
     * @param date:
     * @return Result
     * @description  用来保存医生的id和时间到session中
     */
    @Override
    public Result idDate(String account, String date) {
        List<String> currentTimes = getCurrentTimes();
        int i=1;
        Date date1=null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1=dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DoctorDate doctorDate = new DoctorDate(account, date1);
        System.out.println(doctorDate);
        WebUtil.getSession().setAttribute("doctorDate",doctorDate);
        return new Result(200,"成功");
    }

    @Override
    public Result addduty(CountPrice countPrice) {
        int sectionId = (int) WebUtil.getSession().getAttribute("sectionId");
        DoctorDate doctorDate = (DoctorDate) WebUtil.getSession().getAttribute("doctorDate");
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        String acountId = doctorDate.getId();
        Date date = doctorDate.getDate();
        System.out.println("date______________________------------"+date);
        int num = countPrice.getNum();
        BigDecimal price = countPrice.getPrice();
        Duty duty = new Duty();
        duty.setSectionId(sectionId);
        duty.setAccount(acountId);
        duty.setStuyDatatime(date);
        duty.setDefaultNum(num);
        duty.setPrice(price);
        Date currentDate = new Date(System.currentTimeMillis());
        duty.setCreateTime(currentDate);
        duty.setCreateAccount("超级管理员");
        System.out.println("===================================________________-");
        System.out.println(duty);
        dutyMapper.insertDuty(duty);
        return new Result(200,"完成");
    }

    //时间的加工
    private String getNextDay(Date date,int i){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,i);
        return dateFormat.format(calendar.getTime());
    }
}
